package com.cskaoyan.service;

import com.cskaoyan.bean.MarketUser;
import com.cskaoyan.bean.MarketUserExample;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.mapper.MarketUserMapper;
import com.cskaoyan.util.MyBatisUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

/**
 * @author stone
 * @date 2023/04/05 15:27
 */
public class UserServiceImpl implements UserService {
    @Override
    public CommonData query(Integer page, Integer limit, String sort, String order) {
        MarketUserMapper marketUserMapper = MyBatisUtil.getMapper(MarketUserMapper.class);
        PageHelper.startPage(page, limit);
        MarketUserExample example = new MarketUserExample();
        example.setOrderByClause(sort + " " + order);
        List<MarketUser> users = marketUserMapper.selectByExample(example);
        PageInfo<MarketUser> pageInfo = new PageInfo<>(users);
        /*CommonData data = new CommonData();
        data.setTotal((int) pageInfo.getTotal());
        data.setPages(pageInfo.getPages());
        data.setPage(pageInfo.getPageNum());
        data.setLimit(pageInfo.getPageSize());
        data.setList(pageInfo.getList());
        return data;*/
        return CommonData.data(pageInfo);
    }

    @Override
    public int update(MarketUser marketUser) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        MarketUserMapper mapper = sqlSession.getMapper(MarketUserMapper.class);
        mapper.updateByPrimaryKeySelective(marketUser);
        return 0;
    }

    @Override
    public MarketUser selectById(Integer id) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        MarketUserMapper mapper = sqlSession.getMapper(MarketUserMapper.class);
        MarketUser marketUser = mapper.selectByPrimaryKey(id);
        return marketUser;
    }

    @Override
    public MarketUser selectByMobile(String mobile) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        MarketUserMapper mapper = sqlSession.getMapper(MarketUserMapper.class);
        MarketUser marketUser = mapper.selectByMobile(mobile);
        // System.out.println(marketUser);
        return marketUser;
    }

    @Override
    public List<MarketUser> selectByFuzzyName(String mohu) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        MarketUserMapper mapper = sqlSession.getMapper(MarketUserMapper.class);
        List<MarketUser> userList = mapper.selectByFuzzyName("a");
        System.out.println("userList.size() = " + userList.size());
        return userList;
    }

}
